import {validation,validationCode,LoginStatus} from '../../network/http'
Page({
  data:{
    PhoneNum:'',
    vcode:'',
    sendTime: '获取验证码',
    sendColor: '#363636',
    snsMsgWait: 60
  },
 onLoad(options){

  },
  PhoneNum(e){
    this.setData({
      PhoneNum:e.detail
    })

  },
  getVcode(e){
    this.setData({
      vcode:e.detail
    })
  },
 async sentcode(){
  const {data:res}=await validation(this.data.PhoneNum)
  if(res.code==200){
    this.loginCode()
  }
  console.log(res);
  },
  login(){
    let {PhoneNum,vcode}=this.data;
    // 2. 前端验证 : 不能为空，号码正确
    if(!PhoneNum){
      // 提醒用户
      wx.showToast({
        title:"用户名不能为空！",
        icon:"none"
      });
      return;
    }
    // 正则表达式：号码正确
    let phoneReg=/^1(3|4|5|6|7|8|9)\d{9}$/;
    if(!phoneReg.test(PhoneNum)){
      // 提醒用户
      wx.showToast({
        title:"手机号格式错误！！",
        icon:"none"
      });
      return;
    }
     // 提醒用户
     wx.showToast({
      title:"登陆成功",
    }).then(async ()=>{
    const {data:res}=await validationCode(PhoneNum,vcode)
    const {data:result}=await LoginStatus()
    console.log(result.data); 
    if(res.code===200 && res.data){
      wx.switchTab({
        url: `/pages/personal/personal?id=${result.data.account.id}`,
        
      })
    }
    })
  },
  loginCode(){
    // 60秒后重新获取验证码
    var inter = setInterval(function() {
      this.setData({
        smsFlag: true,
        sendColor: '#cccccc',
        sendTime: this.data.snsMsgWait + 's后重发',
        snsMsgWait: this.data.snsMsgWait - 1
      });
      if (this.data.snsMsgWait < 0) {
        clearInterval(inter)
        this.setData({
          sendColor: '#363636',
          sendTime: '获取验证码',
          snsMsgWait: 60,
          smsFlag: false
        });
      }
    }.bind(this), 1000);

  
  }
  
})